package com.matrix.product.converter;

import com.matrix.api.product.dto.ProductCategoryDTO;
import com.matrix.api.product.dto.ProductDTO;
import com.matrix.api.product.vo.ProductCategoryVO;
import com.matrix.api.product.vo.ProductVO;
import com.matrix.api.product.vo.SpecGroupVO;
import com.matrix.api.product.vo.SpecOptionVO;
import com.matrix.product.entity.Product;
import com.matrix.product.entity.ProductCategory;
import com.matrix.product.entity.ProductSpecGroup;
import com.matrix.product.entity.ProductSpecOption;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValuePropertyMappingStrategy;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author 有点甜
 * @since 2025/8/5
 */
@Mapper(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface ProductConverter {

    ProductConverter INSTANCE = Mappers.getMapper(ProductConverter.class);

    @Mapping(target = "categoryName", ignore = true)
    @Mapping(target = "specs", ignore = true)
    ProductVO toProductVO(Product product);

    List<ProductVO> toProductVOList(List<Product> products);

    @Mapping(target = "products", ignore = true)
    ProductCategoryVO toProductCategoryVO(ProductCategory category);

    List<ProductCategoryVO> toProductCategoryVOList(List<ProductCategory> categories);

    @Mapping(source = "productId", target = "productId")
    @Mapping(target = "options", ignore = true)
    SpecGroupVO toSpecGroupVO(ProductSpecGroup group);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "shopId", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "isDeleted", ignore = true)
    ProductCategory toProductCategory(ProductCategoryDTO dto);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "shopId", ignore = true)
    @Mapping(target = "monthlySales", ignore = true)
    @Mapping(target = "stock", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "isDeleted", ignore = true)
    Product toProduct(ProductDTO dto);

    SpecOptionVO toSpecOptionVO(ProductSpecOption option);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "shopId", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "isDeleted", ignore = true)
    void updateProductCategoryFromDTO(ProductCategoryDTO dto, @MappingTarget ProductCategory category);

    @Mapping(target = "id", ignore = true)
    @Mapping(target = "shopId", ignore = true)
    @Mapping(target = "stock", ignore = true)
    @Mapping(target = "monthlySales", ignore = true)
    @Mapping(target = "createTime", ignore = true)
    @Mapping(target = "updateTime", ignore = true)
    @Mapping(target = "isDeleted", ignore = true)
    void updateProductFromDTO(ProductDTO dto, @MappingTarget Product product);
}
